package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultData;
import com.net.common.util.LocalDateUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.admin.vo.CompanyDailyStatVo;
import com.net.xpay.common.domain.stat.CompanyDailyStat;
import com.net.xpay.common.helper.RedisHelper;
import com.net.xpay.common.manager.stat.CompanyDailyStatManager;
import com.net.xpay.common.model.CompanyDailyStatModel;
import com.net.xpay.common.service.CompanyDailyStatService;
import com.net.xpay.core.constant.RedisKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/company-daily-stat")
@Api(tags = "运营/后台经营数据统计", description = "运营 后台经营数据统计 net")
public class AdminCompanyDailyStatController {

    @Autowired
    private CompanyDailyStatService companyDailyStatService;

    @Autowired
    private CompanyDailyStatManager companyDailyStatManager;

    @Autowired
    private RedisHelper redisHelper;

//    @ApiOperation("按日期范围统计")
//    @PostMapping("/sum")
//    @ResponseBody
//    @PreAuthorize("hasAnyRole('ADMIN', 'company-daily-stat::sum')")
//    public ResultData<CompanyDailyStatVo> sumCompanyDailyStat(@RequestBody CompanyDailyStatModel model) {
//        CompanyDailyStatVo vo = new CompanyDailyStatVo();
//        CompanyDailyStat companyDailyStat = new CompanyDailyStat();
//        //总
//        if (model.getDataType() == 0) {
//            //有缓存从缓存中取
//            Object statCache = redisHelper.get(RedisKey.COMPANY_HISTORY_DATA_STAT);
//            if (statCache != null) {
//                companyDailyStat = SerializationUtil.str2Object(statCache.toString(), CompanyDailyStat.class);
//            } else {
//                companyDailyStat = companyDailyStatService.statCompanyDailyStatByTime(null,LocalDateUtil.getDayEnd(LocalDateTime.now().minusDays(1L)));
//                redisHelper.set(RedisKey.COMPANY_HISTORY_DATA_STAT, SerializationUtil.obj2String(companyDailyStat));
//            }
//
//        }
//       //昨日
//        if (model.getDataType() == 1) {
//            companyDailyStat = companyDailyStatManager.getByDay(LocalDate.now().minusDays(1L));
//            if (companyDailyStat == null){
//                return new ResultData<>(vo);
//            }
//        }
//        //月
//        if (model.getDataType() == 2) {
//            //有缓存从缓存中取
//            Object statCache = redisHelper.get(RedisKey.COMPANY_MONTH_DATA_STAT);
//            if (statCache != null) {
//                companyDailyStat = SerializationUtil.str2Object(statCache.toString(), CompanyDailyStat.class);
//            } else {
//                companyDailyStat = companyDailyStatService.statCompanyDailyStatByTime(LocalDateUtil.getMonthStartDay(LocalDateTime.now()), LocalDateUtil.getDayEnd(LocalDateTime.now().minusDays(1L)));
//                redisHelper.set(RedisKey.COMPANY_MONTH_DATA_STAT, SerializationUtil.obj2String(companyDailyStat));
//            }
//        }
//
//        //计算信息
//        //平均费率  服务费收入/付款成功金额
//        String averageRate = null;
//        if (companyDailyStat.getTotalSuccessTransAmount().compareTo(new BigDecimal("0")) != 0) {
//            averageRate = companyDailyStat.getTotalServicePrice().multiply(new BigDecimal("100")).divide(companyDailyStat.getTotalSuccessTransAmount(), 2, RoundingMode.HALF_UP).toString();
//        }
//
//
//        //批量有密支付成功率
//        String payTransSuccessRate = null;
//        if (companyDailyStat.getPayTransCount() != 0L) {
//            payTransSuccessRate = new BigDecimal(companyDailyStat.getPayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyDailyStat.getPayTransCount()), 2, RoundingMode.HALF_UP).toString();
//        }
//
//
//        //安全发支付成功率
//        String safePaySuccessRate = null;
//        if (companyDailyStat.getSafePayTransCount() != 0L){
//            safePaySuccessRate = new BigDecimal(companyDailyStat.getSafePayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyDailyStat.getSafePayTransCount()), 2, RoundingMode.HALF_UP).toString();
//        }
//
//        vo.setAverageRate(averageRate);
//        vo.setPayTransSuccessRate(payTransSuccessRate);
//        vo.setSafePaySuccessRate(safePaySuccessRate);
//
//        vo.setServiceChargeFee(companyDailyStat.getServiceChargeFee());
//        vo.setRegisterCount(companyDailyStat.getRegisterCount());
//        vo.setTotalCount(companyDailyStat.getTotalCount());
//        vo.setTotalSuccessCount(companyDailyStat.getTotalSuccessCount());
//        vo.setTotalFailCount(companyDailyStat.getTotalFailCount());
//        vo.setTotalSuccessSingleVeryCount(companyDailyStat.getTotalSuccessSingleVeryCount());
//        vo.setTotalFailSingleVeryCount(companyDailyStat.getTotalFailSingleVeryCount());
//        vo.setTotalServicePrice(companyDailyStat.getTotalServicePrice());
//        vo.setTotalTransAmount(companyDailyStat.getTotalTransAmount());
//        vo.setTotalSuccessTransAmount(companyDailyStat.getTotalSuccessTransAmount());
//        vo.setTotalFailTransAmount(companyDailyStat.getTotalFailTransAmount());
//        vo.setTotalBenefit(companyDailyStat.getTotalBenefit());
//        vo.setPayTransCount(companyDailyStat.getPayTransCount());
//        vo.setPayTransSuccessCount(companyDailyStat.getPayTransSuccessCount());
//        vo.setPayTransFailCount(companyDailyStat.getPayTransFailCount());
//
//        vo.setPayTransSuccessSingleVeryCount(companyDailyStat.getPayTransSuccessSingleVeryCount());
//        vo.setPayTransFailSingleVeryCount(companyDailyStat.getPayTransFailSingleVeryCount());
//        vo.setPayTransAmount(companyDailyStat.getPayTransAmount());
//        vo.setPayTransSuccessAmount(companyDailyStat.getPayTransSuccessAmount());
//        vo.setPayTransFailAmount(companyDailyStat.getPayTransFailAmount());
//        vo.setPayTransServicePrice(companyDailyStat.getPayTransServicePrice());
//        vo.setTotalPayTransBenefit(companyDailyStat.getTotalPayTransBenefit());
//        vo.setSafePayTransCount(companyDailyStat.getSafePayTransCount());
//        vo.setSafePayTransSuccessCount(companyDailyStat.getSafePayTransSuccessCount());
//        vo.setSafePayTransFailCount(companyDailyStat.getSafePayTransFailCount());
//        vo.setSafePayTransVeryAlipayUserCount(companyDailyStat.getSafePayTransVeryAlipayUserCount());
//
//        vo.setSafePayTransFailSingleVeryCount(companyDailyStat.getSafePayTransFailSingleVeryCount());
//        vo.setSafePayTransAmount(companyDailyStat.getSafePayTransAmount());
//        vo.setSafePayTransSuccessAmount(companyDailyStat.getSafePayTransSuccessAmount());
//        vo.setSafePayTransFailAmount(companyDailyStat.getSafePayTransFailAmount());
//        vo.setSafePayTransServicePrice(companyDailyStat.getSafePayTransServicePrice());
//        vo.setTotalSafePayTransBenefit(companyDailyStat.getTotalSafePayTransBenefit());
//
//
//        vo.setPayTransActiveUserCount(companyDailyStat.getPayTransActiveUserCount());
//        vo.setPayTransActivePartnerCount(companyDailyStat.getPayTransActivePartnerCount());
//
//        vo.setSafePayActiveUserCount(companyDailyStat.getSafePayActiveUserCount());
//        vo.setSafePayActivePartnerCount(companyDailyStat.getSafePayActivePartnerCount());
//
//        vo.setSafePayPayerAlipayAccountCount(companyDailyStat.getSafePayPayerAlipayAccountCount());
//        vo.setSafePayPayeeAlipayAccountCount(companyDailyStat.getSafePayPayeeAlipayAccountCount());
//
//        vo.setPayTransPayerAlipayAccountCount(companyDailyStat.getPayTransPayerAlipayAccountCount());
//        vo.setPayTransPayeeAlipayAccountCount(companyDailyStat.getPayTransPayeeAlipayAccountCount());
//
//        vo.setSafePayTransSuccessSingleVeryCount(companyDailyStat.getSafePayTransSuccessSingleVeryCount());
//        vo.setPayTransVeryAlipayUserCount(companyDailyStat.getPayTransVeryAlipayUserCount());
//
//
//        return new ResultData<>(vo);
//    }

    //todo 数据处理
    @ApiOperation("按日期范围统计")
    @PostMapping("/sum")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'company-daily-stat::sum')")
    public ResultData<CompanyDailyStatVo> sumCompanyDailyStat(@RequestBody CompanyDailyStatModel model) {
        CompanyDailyStatVo vo = new CompanyDailyStatVo();
        CompanyDailyStat companyDailyStat = new CompanyDailyStat();
        //总
        if (model.getDataType() == 0) {
            //有缓存从缓存中取
            Object statCache = redisHelper.get(RedisKey.COMPANY_HISTORY_DATA_STAT);
            if (statCache != null) {
                companyDailyStat = SerializationUtil.str2Object(statCache.toString(), CompanyDailyStat.class);
            } else {
                companyDailyStat = companyDailyStatService.statCompanyDailyStatByTime(null,LocalDateUtil.getDayEnd(LocalDateTime.now().minusDays(1L)));
                redisHelper.set(RedisKey.COMPANY_HISTORY_DATA_STAT, SerializationUtil.obj2String(companyDailyStat));
            }

        }
        //昨日
        if (model.getDataType() == 1) {
            companyDailyStat = companyDailyStatManager.getByDay(LocalDate.now().minusDays(1L));
            if (companyDailyStat == null){
                return new ResultData<>(vo);
            }
        }
        //月
        if (model.getDataType() == 2) {
            //有缓存从缓存中取
            Object statCache = redisHelper.get(RedisKey.COMPANY_MONTH_DATA_STAT);
            if (statCache != null) {
                companyDailyStat = SerializationUtil.str2Object(statCache.toString(), CompanyDailyStat.class);
            } else {
                companyDailyStat = companyDailyStatService.statCompanyDailyStatByTime(LocalDateUtil.getMonthStartDay(LocalDateTime.now()), LocalDateUtil.getDayEnd(LocalDateTime.now().minusDays(1L)));
                redisHelper.set(RedisKey.COMPANY_MONTH_DATA_STAT, SerializationUtil.obj2String(companyDailyStat));
            }
        }

        //计算信息
        //平均费率  服务费收入/付款成功金额
        String averageRate = null;
        if (companyDailyStat.getTotalSuccessTransAmount().compareTo(new BigDecimal("0")) != 0) {
            averageRate = companyDailyStat.getTotalServicePrice().multiply(new BigDecimal("100")).divide(companyDailyStat.getTotalSuccessTransAmount(), 2, RoundingMode.HALF_UP).toString();
        }


        //批量有密支付成功率
        String payTransSuccessRate = null;
        if (companyDailyStat.getPayTransCount() != 0L) {
            payTransSuccessRate = new BigDecimal(companyDailyStat.getPayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyDailyStat.getPayTransCount()), 2, RoundingMode.HALF_UP).toString();
        }


        //安全发支付成功率
        String safePaySuccessRate = null;
        if (companyDailyStat.getSafePayTransCount() != 0L){
            safePaySuccessRate = new BigDecimal(companyDailyStat.getSafePayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyDailyStat.getSafePayTransCount()), 2, RoundingMode.HALF_UP).toString();
        }

        vo.setAverageRate(averageRate);
        vo.setPayTransSuccessRate(payTransSuccessRate);
        vo.setSafePaySuccessRate(safePaySuccessRate);

        vo.setServiceChargeFee(companyDailyStat.getServiceChargeFee());
        vo.setRegisterCount(companyDailyStat.getRegisterCount());
        vo.setTotalCount(companyDailyStat.getTotalCount());
        vo.setTotalSuccessCount(companyDailyStat.getTotalSuccessCount());
        vo.setTotalFailCount(companyDailyStat.getTotalFailCount());
        vo.setTotalSuccessSingleVeryCount(companyDailyStat.getTotalSuccessSingleVeryCount());
        vo.setTotalFailSingleVeryCount(companyDailyStat.getTotalFailSingleVeryCount());
        vo.setTotalServicePrice(companyDailyStat.getTotalServicePrice().multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
        vo.setTotalTransAmount(companyDailyStat.getTotalTransAmount());
        vo.setTotalSuccessTransAmount(companyDailyStat.getTotalSuccessTransAmount());
        vo.setTotalFailTransAmount(companyDailyStat.getTotalFailTransAmount());
        vo.setTotalBenefit(companyDailyStat.getTotalBenefit());
        vo.setPayTransCount(companyDailyStat.getPayTransCount());
        vo.setPayTransSuccessCount(companyDailyStat.getPayTransSuccessCount());
        vo.setPayTransFailCount(companyDailyStat.getPayTransFailCount());

        vo.setPayTransSuccessSingleVeryCount(companyDailyStat.getPayTransSuccessSingleVeryCount());
        vo.setPayTransFailSingleVeryCount(companyDailyStat.getPayTransFailSingleVeryCount());
        vo.setPayTransAmount(companyDailyStat.getPayTransAmount());
        vo.setPayTransSuccessAmount(companyDailyStat.getPayTransSuccessAmount());
        vo.setPayTransFailAmount(companyDailyStat.getPayTransFailAmount());
        vo.setPayTransServicePrice(companyDailyStat.getPayTransServicePrice().multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
        vo.setTotalPayTransBenefit(companyDailyStat.getTotalPayTransBenefit());
        vo.setSafePayTransCount(companyDailyStat.getSafePayTransCount());
        vo.setSafePayTransSuccessCount(companyDailyStat.getSafePayTransSuccessCount());
        vo.setSafePayTransFailCount(companyDailyStat.getSafePayTransFailCount());
        vo.setSafePayTransVeryAlipayUserCount(companyDailyStat.getSafePayTransVeryAlipayUserCount());

        vo.setSafePayTransFailSingleVeryCount(companyDailyStat.getSafePayTransFailSingleVeryCount());
        vo.setSafePayTransAmount(companyDailyStat.getSafePayTransAmount());
        vo.setSafePayTransSuccessAmount(companyDailyStat.getSafePayTransSuccessAmount());
        vo.setSafePayTransFailAmount(companyDailyStat.getSafePayTransFailAmount());
        vo.setSafePayTransServicePrice(companyDailyStat.getSafePayTransServicePrice().multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
        vo.setTotalSafePayTransBenefit(companyDailyStat.getTotalSafePayTransBenefit());


        vo.setPayTransActiveUserCount(companyDailyStat.getPayTransActiveUserCount());
        vo.setPayTransActivePartnerCount(companyDailyStat.getPayTransActivePartnerCount());

        vo.setSafePayActiveUserCount(companyDailyStat.getSafePayActiveUserCount());
        vo.setSafePayActivePartnerCount(companyDailyStat.getSafePayActivePartnerCount());

        vo.setSafePayPayerAlipayAccountCount(companyDailyStat.getSafePayPayerAlipayAccountCount());
        vo.setSafePayPayeeAlipayAccountCount(companyDailyStat.getSafePayPayeeAlipayAccountCount());

        vo.setPayTransPayerAlipayAccountCount(companyDailyStat.getPayTransPayerAlipayAccountCount());
        vo.setPayTransPayeeAlipayAccountCount(companyDailyStat.getPayTransPayeeAlipayAccountCount());

        vo.setSafePayTransSuccessSingleVeryCount(companyDailyStat.getSafePayTransSuccessSingleVeryCount());
        vo.setPayTransVeryAlipayUserCount(companyDailyStat.getPayTransVeryAlipayUserCount());

        vo.setTotalTransBankAmount(companyDailyStat.getTotalTransBankAmount());
        vo.setTotalTransAlipayAmount(companyDailyStat.getTotalTransAlipayAmount());
        vo.setTotalTransAlipayAmount(companyDailyStat.getTotalTransAlipayAmount());
        vo.setTotalSuccessTransAlipayAmount(companyDailyStat.getTotalSuccessTransAlipayAmount());

        vo.setSafePayTransBankAmount(companyDailyStat.getSafePayTransBankAmount());
        vo.setSafePayTransAlipayAmount(companyDailyStat.getSafePayTransAlipayAmount());
        vo.setSafePayTransSuccessBankAmount(companyDailyStat.getSafePayTransSuccessBankAmount());
        vo.setSafePayTransSuccessAlipayAmount(companyDailyStat.getSafePayTransSuccessAlipayAmount());
        return new ResultData<>(vo);
    }

}
